package com.blackcat.recode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackcat.recode.common.result.AjaxResult;
import com.blackcat.recode.dao.DeployRecodeMapper;
import com.blackcat.recode.entity.DeployRecode;
import com.blackcat.recode.entity.UpdateRecode;
import com.blackcat.recode.dao.UpdateRecodeMapper;
import com.blackcat.recode.entity.Worker;
import com.blackcat.recode.service.UpdateRecodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blackcat.recode.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p> 更新记录表 服务实现类
 * @author blackcat 2024-09-10
 */
@Service
public class UpdateRecodeServiceImpl extends ServiceImpl<UpdateRecodeMapper, UpdateRecode> implements UpdateRecodeService {

    @Autowired
    private UpdateRecodeMapper updateRecodeMapper;
    @Autowired
    private DeployRecodeMapper deployRecodeMapper;

    @Override
    public AjaxResult remove(String id) {
        updateRecodeMapper.deleteById(id);
        QueryWrapper<DeployRecode> deployRecodeQueryWrapper = new QueryWrapper<>();
        deployRecodeQueryWrapper.inSql("update_id", "select id from update_recode where branch_id='"+id+"'");
        deployRecodeMapper.delete(deployRecodeQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getVersionList(String branchId, String serviceId, String projectId) {
        QueryWrapper<UpdateRecode> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(branchId)) {
            queryWrapper.eq("branch_id", branchId);
        }
        if (StringUtils.isNotBlank(serviceId)) {
            queryWrapper.eq("service_id", serviceId);
        }
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        queryWrapper.orderByDesc("create_time");
        List<UpdateRecode> recodeList = updateRecodeMapper.selectList(queryWrapper);
        return AjaxResult.success(recodeList);
    }

    @Override
    public AjaxResult getList(Integer pageNow, Integer size, String versionNo, String branchId, String serviceId, String projectId) {
        QueryWrapper<UpdateRecode> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(versionNo)) {
            queryWrapper.like("version_no", versionNo);
        }
        if (StringUtils.isNotBlank(branchId)) {
            queryWrapper.eq("branch_id", branchId);
        }
        if (StringUtils.isNotBlank(serviceId)) {
            queryWrapper.eq("service_id", serviceId);
        }
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        queryWrapper.orderByDesc("create_time");
        Page<UpdateRecode> page = new Page<>(pageNow, size);
        Page<UpdateRecode> result = updateRecodeMapper.selectPage(page, queryWrapper);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult edit(UpdateRecode updateRecode) {
        System.out.println("updateRecode "+updateRecode.getUpdateExplain());
        if (updateRecode.getId() == null) {
            updateRecodeMapper.insert(updateRecode);
        }else {
            updateRecodeMapper.updateById(updateRecode);
        }
        return AjaxResult.success();
    }
}
